package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.mapper.AdminMapper;
import com.imooc.pojo.Admin;
import com.imooc.pojo.bo.AdminBO;
import com.imooc.service.AdminService;
import com.imooc.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author 椋庨棿褰辨湀
 * @since 2025-09-10
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

	@Autowired
	private AdminMapper adminMapper;


	@Override
	public boolean adminLogin(AdminBO adminBO) {
		Admin selfAdmin = getSelfAdmin(adminBO.getUsername());
		if(selfAdmin == null ){
			return false;
		}
		String slat = selfAdmin.getSlat(); //获取密码的盐
		String md5Str = MD5Utils.encrypt(adminBO.getPassword(), slat);
		if(selfAdmin.getPassword().equalsIgnoreCase(md5Str)){
			return true;
		}
		return false;
	}

	@Override
	public Admin getAdminInfo(AdminBO adminBO) {
		return getSelfAdmin(adminBO.getUsername());
	}

	private Admin getSelfAdmin(String username) {
		Admin admin = adminMapper.selectOne(
				new QueryWrapper<Admin>()
						.eq("username", username)
		);
		return admin;
	}

}
